package dfsketches.gr3d;

import java.util.Map.Entry;
import java.util.NavigableSet;
import java.util.TreeMap;

public class SplineConstructor {

	private TreeMap<Float, Matrix3D> points;
	private SplineType type;
	private boolean isLooped;
	
	public SplineConstructor(SplineType splineType) {
		this(splineType, false);
	}
	
	public SplineConstructor(SplineType splineType, boolean looped) {
		type = splineType;
		points = new TreeMap<Float, Matrix3D>();
		isLooped = looped;
	}

	public void addPosition(float time, Matrix3D transformation) {
		points.put(time, transformation);
	}
	
	public void removePositionsBetween(float time1, float time2) {
		NavigableSet<Float> keySet = points.navigableKeySet(); 
		Float t1 = keySet.floor(time1);
		Float t2 = keySet.floor(time2);
		keySet.removeAll(keySet.subSet(t1, t2));
	}
	
	public Spline construct() {
		SplinePoint[] pts = new SplinePoint[points.size()];
		int i = 0;
		for (Entry<Float, Matrix3D> entry : points.entrySet()) {
			pts[i++] = new SplinePoint(entry.getKey(), entry.getValue());
		}
		if (type == SplineType.Linear) {
			return new LinearSpline(pts, isLooped);
		} else {
			return new LinearSpline(pts);	//TODO return Discrete or Zero spline
		}
	}
	
}
